package com.cheung.leetcode.arr;

import  org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.arr
 * @Author :  cheungming
 * @CreateTime : 2024-07-24 11:11:55
 * @Description : 238 . 除自身以外数组的乘积
 * @Version : 1.0
 */
public class Code238Test {

    public int[] productExceptSelf(int[] nums) {
        int[] res = new int[nums.length];
        res[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            res[i] = nums[i - 1] * res[i - 1];
        }

        int right = 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            res[i] = res[i] * right;
            right = right * nums[i];
        }

        return res;
    }

    @Test
    public void test1() {
        assert Arrays.equals(new int[]{24, 12, 8, 6}, productExceptSelf(new int[]{1, 2, 3, 4}));
    }

    @Test
    public void test2() {
        assert Arrays.equals(new int[]{0, 0, 9, 0, 0}, productExceptSelf(new int[]{-1, 1, 0, -3, 3}));
    }

}
